SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


ALTER   procedure p_checkbillrelationqty (
  @btype int,
  @bid int,
  @bno int,
  @return int output
)  as

begin
  set nocount on
  set @return=0
  if (@btype is null or @bid is null or @bno is null) return

  if (@btype=130 and  exists(select * from tb_purvaluedtl where bid=@bid and bno=@bno and abs(quantity)-abs(relationqty)<0)) 
  or (@btype=240 and  exists(select * from tb_saledtl where bid=@bid and bno=@bno and abs(quantity)-abs(relationqty)<0)) 
  or (@btype=140 and  exists(select * from tb_purchasedtl where bid=@bid and bno=@bno and abs(quantity)-abs(relationqty)<0))
--  or (@btype=230 and  exists(select * from tb_saleconsigndtl where bid=@bid and bno=@bno and abs(quantity)-abs(relationqty+balqty)<0)) 
  or (@btype=220 and  exists(select * from tb_salesendoutdtl where bid=@bid and bno=@bno and abs(quantity)-abs(relationqty+balqty)<0)) 
    set @return=1
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

